package com.xiao.webtest.dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.util.List;

/**
 * Created by Administrator on 2014/11/15.
 */
@Repository
public class BaseDao{

    private Log log = LogFactory.getLog(BaseDao.class);
    private JdbcTemplate jdbcTemplate;
    
    @Autowired
    public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	/**
     * 鏇存柊鎿嶄綔
     *
     * @param namedSql
     * @param javaBean
     * @return
     */
    @com.xiao.webtest.DataSource("write")
    protected int update(String namedSql, Object javaBean) {
        SqlParameterSource paramSource = new BeanPropertySqlParameterSource(
                javaBean);
        return this.jdbcTemplate.update(namedSql, paramSource);
    }
    /**
     * 鎵归噺鏇存柊
     *
     * @param sql
     * @param paramValue
     * @return
     */
    @com.xiao.webtest.DataSource("write")
    protected int commonUpdate(String sql, Object... paramValue) {
        return this.jdbcTemplate.update(sql, paramValue);
    }
    /**
     * 鑾峰彇count璁℃暟
     *
     * @param sql
     * @return
     */
    protected int getCount(String sql) {
        return this.jdbcTemplate.queryForObject(sql, Integer.class);
    }
    /**
     * 鑾峰彇涓�釜bean瀹炰綋
     *
     * @param sql
     * @param returnType
     * @return
     */
    @com.xiao.webtest.DataSource("read")
    protected <T> T getJavaBean(String sql, Class<T> returnType) {
        RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(returnType);
        log.info(sql);
        try {
            return this.jdbcTemplate.queryForObject(sql, rowMapper);
        } catch (Exception ex) {
            return null;
        }
    }
    /**
     * 鑾峰彇list
     *
     * @param sql
     * @param returnType
     * @return
     */
    @com.xiao.webtest.DataSource("read")
    protected <T> List<T> getList(String sql, Class<T> returnType) {
        RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(returnType);
        log.info(sql);
        return this.jdbcTemplate.query(sql, rowMapper);
    }
    protected <T> List<T> getList(String sql, Class<T> returnType,
                                  Object... paramValue) {
        RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(returnType);
        return this.jdbcTemplate.query(sql, rowMapper, paramValue);
    }
    /**
     * 鍒嗛〉鑾峰彇list
     *
     * @param
     * @param returnType
     * @return
     */
    protected <T> List<T> getPageList(String querySQL, Class<T> returnType,
                                      int pageNumber, int pageSize) {
        RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(returnType);
        StringBuffer sqlBuffer = new StringBuffer();
        sqlBuffer.append("select * from ( ");
        sqlBuffer.append(querySQL);
        sqlBuffer.append(" ) as t limit " + pageNumber + "," + pageSize);
        log.info(sqlBuffer.toString());
        return this.jdbcTemplate.query(sqlBuffer.toString(), rowMapper);
    }


}
